<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <login-setting :user-detail="userDetail" @updateEmpDetailEV="updateFn" />
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <user-info ref="userInfo" @updateUserInfoEV="updateUserInfoFn" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import LoginSetting from './components/login-setting.vue'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
import { getUserDetailByAPI, saveUserDetailByAPI } from '@/api/employees'

export default {
  name: 'EmployeesDetail',
  components: {
    LoginSetting,
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userDetail: {} // 员工详情
    }
  },
  created() {
    this.getUserDetailFn()
  },
  methods: {
    // 获取-员工详情
    async getUserDetailFn() {
      const res = await getUserDetailByAPI(this.$route.query.id)
      console.log(res)

      // 方式1: 直接给子组件data赋值
      // 组织架构: 直接给子组件data赋值
      // this.$refs.别名拿到子组件对象.内部data名字直接赋值
      this.$refs.userInfo.userInfo = res.data

      // 方式2: 传统的方式, 父->子 (然后子里把props的值赋予给data)
      this.userDetail = res.data
    },
    // 更新-员工详情(姓名/密码)
    async updateFn(dataObj) {
      const res = await saveUserDetailByAPI(dataObj)
      // console.log(res)
      this.getUserDetailFn() // 重新获取一次详情数据->再次回显(有点多余)
      this.$message.success(res.message) // 提示成功
    },
    // 更新-员工详情(工号/其他...)
    async updateUserInfoFn(dataObj) {
      const res = await saveUserDetailByAPI(dataObj)
      this.getUserDetailFn() // 重新获取一次详情数据->再次回显(有点多余)
      this.$message.success(res.message) // 提示成功
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
